package com.lenovo.smarttraffic.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;

public class LoginMyActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView goback;
    private ImageView setting;
    private EditText editTextName;
    private TextView userNameNotEmpty;
    private TextInputLayout textInputLayoutName;
    private EditText editTextPassword;
    private TextView passworNotEmpty;
    private TextInputLayout textInputLayoutPassword;
    private CheckBox jzpwdCB;
    private CheckBox autologCB;
    private Button loginBtn;
    private Button regBtn;
    public static boolean islogin = false;
    public static boolean remeberPassword = false;

    static String password;
    static String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_my);
        initView();

        if(remeberPassword){
            editTextName.setText(userName);
            editTextPassword.setText(password);
            jzpwdCB.setChecked(true);
        }
    }

    private void initView() {
        goback = (ImageView) findViewById(R.id.goback);
        setting = (ImageView) findViewById(R.id.setting);
        editTextName = (EditText) findViewById(R.id.editTextName);
        userNameNotEmpty = (TextView) findViewById(R.id.userNameNotEmpty);
        textInputLayoutName = (TextInputLayout) findViewById(R.id.textInputLayoutName);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        passworNotEmpty = (TextView) findViewById(R.id.passworNotEmpty);
        textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
        jzpwdCB = (CheckBox) findViewById(R.id.jzpwdCB);
        autologCB = (CheckBox) findViewById(R.id.autologCB);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        regBtn = (Button) findViewById(R.id.regBtn);

        loginBtn.setOnClickListener(this);
        regBtn.setOnClickListener(this);

        //是否记住密码
        jzpwdCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                LoginMyActivity.remeberPassword = b;
            }
        });

        Context context = this ;
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context,NetWorkActivity.class));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginBtn:
                submit();
                break;
            case R.id.regBtn:
                break;
        }
    }

    private void submit() {
        // validate
        String editTextNameString = editTextName.getText().toString().trim();
        if (TextUtils.isEmpty(editTextNameString)) {
            Toast.makeText(this, "editTextNameString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String editTextPasswordString = editTextPassword.getText().toString().trim();
        if (TextUtils.isEmpty(editTextPasswordString)) {
            Toast.makeText(this, "editTextPasswordString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        boolean a=false,b=false;
        if(editTextNameString.equals("")){
            userNameNotEmpty.setVisibility(View.VISIBLE);
        }else {
            a=true;
        }

        if(editTextPasswordString.equals("")){
            passworNotEmpty.setVisibility(View.VISIBLE);
        }else {
            b=true;
        }

        if(a == false || b==false){
            return;
        }else {
            islogin = true;

            //记住密码了以后保存当前密码
            if(remeberPassword){
                password = editTextPasswordString;
                userName = editTextNameString;
            }

            startActivity(new Intent(this,NetWorkActivity.class));
        }

        // TODO validate success, do something


    }
}
